﻿//For the search users page

// Initialize jQuery
$(document).ready(function () {
   var ajaxPrefix = "";
    //Searches for a person and displays the page.

    $("#search").click(function () { //Button ID
        var requestReturn = "";

        var name = $('#SearchNamebox').val().toString();      // this is the ID from the HTML Search field (var to scope it to local call)

        var results = jQuery.ajax({
            type: "GET",
            url: "/api/Search/searchUsers/?name=" + escape(name), //this line determines which controller method to call.
            async: false,
        }).responseText;
       // console.log(results);
        requestReturn += "<tr>";
        requestReturn += "<th>User ID  </th>";
        requestReturn += "<th>First Name  </th>";
        requestReturn += "<th>Last Name  </th>";
       // requestReturn += "<th>View Events</th>";
        requestReturn+= "</tr>";

        users = $.parseJSON(results);

        if (users == null||users=="") {
            requestReturn += "<tr>";
            requestReturn += "<td>";
            requestReturn += "No Results for your search";
            requestReturn += "</td>";
            requestReturn += "</tr>";
        }
         else{
        //user- user, contact //family added later
        $.each(users, function (i, user) {
            
                requestReturn += "<td>";
                requestReturn +=  user.user.USER_ID;
                requestReturn += "</td>";
                requestReturn += "<td>";
                requestReturn += user.contact.FIRST_NAME;
                requestReturn += "</td>";
                requestReturn += "<td>";
                requestReturn += user.contact.LAST_NAME;
                requestReturn += "</td>";
                requestReturn += "<td>";
                //requestReturn += '<a href="' + ajaxPrefix + '/Search/searchUserEvents/' + user.user.USER_ID + '">View</a>';
                requestReturn += "</td>";
                requestReturn += "</tr>";
        });//end of $.each
        }//end of else
        $("#foundUsers").html(requestReturn);
    })





   
    //For the event page
    //Searches for the events between the dates and displays the page.
    //name of button, then determines action on button
    $("#eventSearch").click(function () {
        eventReturn = "";

        name = $('#eventName').val();      // this is the ID from the HTML Search field
        fromDate = $('#fromDate').val();    //value from the first date box
        toDate = $('#toDate').val();        //value from the second date box

        eventReturn += "<tr>";
        eventReturn += "<th>Event ID   </th>";
        eventReturn += "<th>Event Name   </th>";
        eventReturn += "<th>Event Start Date  </th>";
        eventReturn += "<th>Event End Date  </th>";
        eventReturn += "</tr>";

        if (name != "" && fromDate != "" & toDate != "") {
            var eventResults = jQuery.ajax({
                url: "/api/Search/searchEventsAll/?eventName=" + escape(name)+"&fromDate="+escape(fromDate)+"&toDate="+escape(toDate),
                type: "GET",
                async: false,
            }).responseText;
           // console.log(results);
          
            events = $.parseJSON(eventResults);
            if (events == "" || events == null) {
                eventReturn += "<tr>";
                eventReturn += "No Results Found";
                eventReturn += "</tr>";
            }
            else {
                $.each(events, function (i, event) {
                    {
                        eventReturn += "<tr>";
                        eventReturn += "<td>";
                        eventReturn += event.eventItem.EVENT_ID + "     ";
                        eventReturn += "</td>";
                        eventReturn += "<td>";
                        eventReturn += event.eventItem.NAME + "     ";
                        eventReturn += "</td>";
                        eventReturn += "<td>";
                        eventReturn += event.eventItem.START_DATE + "    To    ";
                        eventReturn += "</td>";
                        eventReturn += "<td>";
                        eventReturn += event.eventItem.END_DATE;
                        eventReturn += "</td>";
                        eventReturn += "</tr>";
                    }
                });
            }
            $("#foundEvents").html(eventReturn);
       }
        else if (name != "" && fromDate == "" & toDate == "") {
            var results = jQuery.ajax({
                type: "GET",
                url: "/api/Search/searchEvent/?eventNameOnly=" + escape(name),
                async: false,
            }).responseText;

            events = $.parseJSON(results);
            if (events == "" || events == null) {
                eventReturn += "<tr>";
                eventReturn += "No Results Found";
                eventReturn += "</tr>";
            }
            else {
                $.each(events, function (i, event) {
                    {
                        eventReturn += "<tr>";
                        eventReturn += "<td>";
                        eventReturn += event.eventItem.EVENT_ID + "     ";
                        eventReturn += "</td>";
                        eventReturn += "<td>";
                        eventReturn += event.eventItem.NAME + "     ";
                        eventReturn += "</td>";
                        eventReturn += "<td>";
                        eventReturn += event.eventItem.START_DATE + "  To  ";
                        eventReturn += "</td>";
                        eventReturn += "<td>";
                        eventReturn += event.eventItem.END_DATE;
                        eventReturn += "</td>";
                        eventReturn += "</tr>";
                    }
                });
            }
            $("#foundEvents").html(eventReturn);
        }
        else if (name == "" && fromDate != "" & toDate != "") {
            var results = jQuery.ajax({
                type: "GET",
                url: "/api/Search/searchEvents/?fromDate1=" + escape(fromDate) + "&toDate1=" + escape(toDate),
                async: false,
            }).responseText;
           // console.log(results);
            events = $.parseJSON(results);
            if (events == "" || events == null) {
                eventReturn += "<tr>";
                eventReturn += "No Results Found";
                eventReturn += "</tr>";
            }
            else {
                $.each(events, function (i, event) {
                    {
                        eventReturn += "<tr>";
                        eventReturn += "<td>";
                        eventReturn += event.eventItem.EVENT_ID + "     ";
                        eventReturn += "</td>";
                        eventReturn += "<td>";
                        eventReturn += event.eventItem.NAME + "     ";
                        eventReturn += "</td>";
                        eventReturn += "<td>";
                        eventReturn += event.eventItem.START_DATE + "  To  ";
                        eventReturn += "</td>";
                        eventReturn += "<td>";
                        eventReturn += event.eventItem.END_DATE;
                        eventReturn += "</td>";
                        eventReturn += "</tr>";
                    }
                });
                $("#foundEvents").html(eventReturn);
            }
        }
    })






    

    
    /*
    //This is the start of the pagination, not sure if in correct location though.
    currentPage = 1;
    itemsPerPages = 15;

    //sets to page 1 when the page is loaded
    window.onload = function () {
        changePage(1);
    };

    //how many pages there are
    function numberOfPages() {
        return jsonObj / itemsPerPages;
    }

    //call change pages with the next page in line
    function nextPage() {
        if (currentPage < numberOfPages) {
            currentPage++;
            changeCurrentPage(currentPage);
        }
    }

    // call change pages with the previous in line
    function previousPage() {
        if (currentPage < numberOfPages && currentPage > 1) {
            currentPage--;
            changeCurrentPage(currentPage);
        }
    }

    // all of the logic for moving back and forth between pages
    function changeCurrentPage(pageNumber) {

        //create buttons and table for page, need to know what Nate is calling them.
        var next = document.getElementById("Next");
        var Last = document.getElementById("Last");
        var displayTable = document.getElementById("Table name nate is using");
        var spanningPages = document.getElementById("Page");

        //page vallidation makeing sure it doesn't go over or below max pages needed
        if (pageNumber < 1) {
            pageNumber = 1;
        }
        else if (pageNumber > numberOfPages()) {
            page = numberOfPages;
        }

        //change page logic, need to test loop, make sure it doesn't go out of bounds.
        for (i = (pageNumber - 1) * itemsPerPage; i < (itemsPerPages * pageNumber) && i < jsonObj; i++) {
            displayTable.innerHTML += jsonObj[i] + "<br>";
        }

        //hide buttons if either at first or last page so they can't be clicked, looks nicer, also produces less errors
        if (currentPage >= numberOfPages)
            next.style.visibility = "hidden";
        else if (currentPage < numberOfPages)
            next.style.visibility = "visbile";

        if (currentPage <= 1)
            back.style.visibility = "hidden";
        else if (currentPage > 1)
            back.style.visibility = "visible";
    }
    */

    // VCL Add Member to Group
    $("#addMember").click(function () {
        $("#searchUsers").dialog("open");
    });             // end of $("#addMember").click(function () {

    // VCL Dialog box - to add new members via auto complete
    $("#searchUsers").dialog({
        autoOpen: false,
        height: 300,
        width: 600,
        modal: true,
        buttons: {
            "Find Users": function () {


                $(this).dialog("close");
            },
            "Close": function () {
                $(this).dialog("close");
            }
        }
    });         // end of $("#selectUserDialog").dialog({

    $("#addNewAutocomplete").click(function () {
        var html = "";

        html += '<div>';
        html += '<input type="text" class="autocompleteUser" />';
        html += '</div>';
        $("#searchUsers").append(html);

    });     // end of $("#addNewAutocomplete").click(function () {

    $("#autocompleteUser").autocomplete({
        source: "/api/Search/searchUsers/?name=" + escape(name),
        minLength: 3,
        select: function (event, ui) {
            log(ui.item ?
              "Selected: " + ui.item.value + " aka " + ui.item.id :
              "Nothing selected, input was " + this.value);
        }
    });

    $(".eventDatepicker").datepicker();

});// end of $(document).ready(function () { 

